package com.swhysc.information.controller;


import com.swhysc.information.common.model.GridView;
import com.swhysc.information.common.model.ResultBody;
import com.swhysc.information.entity.InformationDataTableEx;
import com.swhysc.information.service.IInformationDataTableExService;
import com.swhysc.information.vo.InformationDataTableExVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @Author zmq
 * @Date 2022/10/13 17:47
 * @Description
 */
@RestController
@RequestMapping("/api/infor/data/table/experiment")
@Api(value = "RelaTimeInfoTableExController", tags = "资讯数据表(实验室)")
@Slf4j
public class InformationDataTableExController {
    @Autowired
    private IInformationDataTableExService iInformationDataTableService;

    @GetMapping("/list")
    @ApiOperation(value = "列表接口", notes = "列表接口")
    public ResultBody<GridView> list(
            @ApiParam("查询参数") @RequestParam(required = false) String queryParam,
            @ApiParam("数据源id") @RequestParam(required = false) String dataSourceIds,
            @ApiParam(value = "页数") @RequestParam Integer page,
            @ApiParam(value = "行数") @RequestParam Integer size) {
        try {
            List list = iInformationDataTableService.getList(queryParam, dataSourceIds, page, size);
            Long count = iInformationDataTableService.getCount(queryParam, dataSourceIds);
            return ResultBody.success(new GridView(list, count));
        } catch (RuntimeException e) {
            return ResultBody.failure("查询失败");
        }
    }

    /**
     * 新增
     *
     * @param
     * @return
     */
    @PostMapping("/create")
    @ApiOperation(value = "新增", notes = "新增")
    public ResultBody create(@RequestBody InformationDataTableEx informationDataTable) {
        Boolean result;
        try {
            result = iInformationDataTableService.create(informationDataTable);
        } catch (Exception e) {
            return ResultBody.failure(e.getMessage());
        }
        return ResultBody.success(result);
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @GetMapping("/delete")
    @ApiOperation(value = "删除", notes = "删除")
    public ResultBody delete(@ApiParam(value = "批量删除") @RequestParam String ids) {
        Map<String, Object> result = null;
        try {
            result = iInformationDataTableService.delete(ids);
        } catch (Exception e) {
            return ResultBody.failure("删除数据表出错");
        }
        return ResultBody.success(result);
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改", notes = "修改")
    public ResultBody update(@RequestBody InformationDataTableEx informationDataTable) {
        Boolean result;
        try {
            result = iInformationDataTableService.update(informationDataTable);
        } catch (Exception e) {
            return ResultBody.failure(e.getMessage());
        }
        return ResultBody.success(result);
    }


    @GetMapping("/getOne")
    @ApiOperation(value = "表详情", notes = "表详情")
    public ResultBody<GridView> getOne(@ApiParam("数据表id") @RequestParam String id) {
        InformationDataTableExVo result;
        try {
            result = iInformationDataTableService.getOne(id);
            return ResultBody.success(result);
        } catch (RuntimeException e) {
            return ResultBody.failure("查询失败");
        }
    }

    /**
     * 根据左侧数据源目录树id获取数据表列表
     * @param dirId 目录Id
     * @param page 页数
     * @param size 每页的个数
     * @return
     */
    @GetMapping("/dirId/list")
    @ApiOperation(value = "获取目录下的所有数据表列表", notes = "目录下的所有数据表列表")
    public ResultBody<GridView> dirIdList(@ApiParam(value = "目录Id") @RequestParam String dirId,
                                          @ApiParam(value = "表名") @RequestParam(required = false) String name,
                                          @ApiParam(value = "页数") @RequestParam(required = false) Integer page,
                                          @ApiParam(value = "每页的个数") @RequestParam(required = false) Integer size) {
        try {
            List list = iInformationDataTableService.getDirList(dirId,page,size,name);
            Long count = iInformationDataTableService.getDirCount(dirId,name);
            return ResultBody.success(new GridView<>(list,count));
        } catch (RuntimeException e) {
            return ResultBody.failure("查询失败");
        }
    }


    @ApiOperation(value = "批量导入")
    @PostMapping("/import")
    public ResultBody indexConfigImport(HttpServletRequest request, HttpServletResponse response, MultipartFile file) {
        Map<String, Object> result = null;
        try {
            result = iInformationDataTableService.importData(request,response,file);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBody.failure("资讯数据导入失败");
        }
        return ResultBody.success(result);
    }

    /**
     * 根据id导出数据表和字段的信息
     * @param response 响应体
     * @param id 数据表id
     */
    @ApiOperation(value = "根据id导出数据表和字段的信息", notes = "根据id导出数据表和字段的信息")
    @GetMapping("/export")
    public void export(HttpServletResponse response, @RequestParam String id) {
        try {
            iInformationDataTableService.export(id, response);
        } catch (Exception e) {
            log.error("导出excel异常", e.getMessage());
            throw  e;
        }
    }


    /**
     * 数据表批量导出
     * 根据目录id选中导出数据表和字段的信息
     * 不选中id 导出全部数据
     * @param response 响应体
     * @param id 目录id
     * @param isAll 1 是  0 否
     */
    @ApiOperation(value = "数据表批量导出", notes = "批量导出数据表批量导出")
    @GetMapping("/AllExport")
    public void AllExport(HttpServletResponse response, @RequestParam(required = false) String id, @RequestParam String isAll) {
        try {
            iInformationDataTableService.AllExport(id, isAll, response);
        } catch (Exception e) {
            log.error("数据表批量导出excel异常", e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 移动更改数据表
     * @return
     */
    @GetMapping("/moveTable")
    @ApiOperation(value = "移动更改数据表", notes = "移动更改数据表")
    public ResultBody moveDir(@ApiParam(value = "要移动的数据表") @RequestParam String id,
                              @ApiParam(value = "目标数据表") @RequestParam String tagId,
                              @ApiParam(value = "移动位置") @RequestParam String moveType) {
        try {
            return ResultBody.success(iInformationDataTableService.moveTable(id,tagId,moveType));
        } catch (Exception e) {
            log.error("【移动表异常】:{}",e.getMessage());
            return ResultBody.failure("移动表失败！");
        }
    }

}
